package com.ruoyi.SuveryInterface.SuveryUtils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;

public class RZBService {

	public static void main(String[] args) {
		//注意：参数必须要经过utf-8编码
		simpleCheckV4();
		//queryBalanceV4();
	}
	//简项认证
	static void simpleCheckV4(){
		String account = "cszx377"; // 账号
		String pwd = "9DRBA2AAWw362DVZjtUXU3j6QPA4Ryz5"; // 密码
		String idNumber = "350627199110183017"; //身份证号
		String name = ""; //姓名
		String sign = md5(md5(idNumber + account) + pwd);
		String url="https://service.sfxxrz.com/simpleCheckV4.ashx";
		String postdata="";
		postdata = "idNumber=" + idNumber
							+ "&name=" +name
							+ "&account=" + account
							+ "&sign=" + sign;

		String json="";
		try {
			json = readContentFromPost(url, postdata);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(json);
	}
	//多项比对
	static void ExactCheckV4(){
		//注意：需要比对什么就填写什么，否则为空，为空就不比对
		String account = "xxx"; // 账号
		String pwd = "xxx"; // 密码
		String idNumber = "xxx"; //身份证号
		String name = "xxx"; //姓名
		String sign = md5(md5(idNumber + account) + pwd);
		String FormerName = "xxxx";//曾用名
        String Sex = "xxxx";//性别
        String Nation = "xxxx";//名族
        String Birthday = "xxxx";//出生日期 格式:yyyymmdd,如19850101
        String Company = "xxxx";//所属单位
        String Education = "xxxx";//教育水平
        String MaritalStatus = "xxxx"; //婚否
        String NativePlace = "xxxx";//籍贯省市县
        String BirthPlace = "xxxx";//出生省市县
        String ssssxq = "xxxx";//所属省市县
        String Address = "xxxx";//住址
		String url="https://service.sfxxrz.com/ExactCheckV4.ashx";
		String postdata="";
		postdata = "idNumber=" + idNumber
							+ "&name=" + name
							+ "&account=" + account
							+ "&FormerName=" + FormerName
							+ "&Sex=" + Sex
							+ "&Nation=" + Nation
							+ "&Birthday=" + Birthday
							+ "&Company=" + Company
							+ "&Education=" + Education
							+ "&MaritalStatus=" + MaritalStatus
							+ "&NativePlace=" + NativePlace
							+ "&BirthPlace=" + BirthPlace
							+ "&ssssxq=" + ssssxq
							+ "&Address=" + Address
							+ "&sign=" + sign;


		String json="";
		try {
			json = readContentFromPost(url, postdata);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(json);
	}

	//余额查询
	static void queryBalanceV4(){
		String account = "xxxx"; //账号
		String pwd = "xxxx"; //密码
		String sign = md5(md5(account) + pwd);
		String url="https://service.sfxxrz.com/queryBalanceV4.ashx";
		String postdata="";
		postdata =  "account=" + account + "&sign=" + sign;
		String json="";
		try {
			json = readContentFromPost(url, postdata);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(json);
	}
	static String md5(String text) {
		byte[] bts;
		try {
			bts = text.getBytes("UTF-8");
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] bts_hash = md.digest(bts);
			StringBuffer buf = new StringBuffer();
			for (byte b : bts_hash) {
				buf.append(String.format("%02X", b & 0xff));
			}
			return buf.toString();
		} catch (java.io.UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		} catch (java.security.NoSuchAlgorithmException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String readContentFromPost(String POST_URL,String postdata) throws IOException{
			URL postUrl = new URL(POST_URL);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
			connection.setDoOutput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
			connection.connect();
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			out.write(postdata.getBytes());
			out.flush();
			out.close();
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));//设置编码,否则中文乱码
			String line="";
			StringBuffer response = new StringBuffer();
			while ((line = reader.readLine()) != null){
				response.append(line);
			}
			reader.close();
			connection.disconnect();
			return response.toString();
		}
}
